﻿using System;
using System.Linq;
using System.Net;
using System.Collections.Generic;
using System.Reflection;

namespace SilverPortal
{
	/// <summary>
	/// Assists with the tracking and loading of assemblies.
	/// </summary>
	internal static class AssemblyManager
	{
		private static List<Assembly> _loadedAssemblies = new List<Assembly>();

		static AssemblyManager()
		{
			_loadedAssemblies.Add(typeof(AssemblyManager).Assembly);
		}

		public static void RegisterAssembly(Assembly assembly)
		{
			_loadedAssemblies.Add(assembly);
		}

		public static Assembly GetAssembly(string name)
		{
			Assembly assembly;
			assembly = _loadedAssemblies.FirstOrDefault(a=> {
				return a.FullName.StartsWith(name + ",");
			});

			if (assembly != null)
				return assembly;

			throw new InvalidOperationException("Assembly " + name + " is not loaded into the AppDomain.");
		}
	}
}
